<template>
    <div>
        <Modal v-model="showModal" width="800" @on-cancel="rightClose" draggable>
            <p slot="header" style="color:#f60;text-align:center">
                <span>增加容忍</span>
            </p>
            <div style="">
                <Form ref="toleration" :model="toleration" :rules="toleration_rules" label-position="left"  :label-width="150">
                    <Form-Item label="选择容忍效果" prop="effect">
                        <Select v-model="toleration.effect">
                            <Option v-for="effect in toleration_effect_list" :key="effect" :value="effect">{{ effect }}</Option>
                        </Select>
                    </Form-Item> 
                    <Form-Item label="key:"  prop="key">
                        <Input type="text"  v-model="toleration.key" />
                    </Form-Item>
                    <Form-Item label="选择operator:" prop="operator">
                        <Select v-model="toleration.operator">
                            <Option v-for="operator in toleration_operator_list" :key="operator" :value="operator">{{ operator }}</Option>
                        </Select>
                    </Form-Item> 
                    <Form-Item label="value:" prop="value" >
                        <Input type="text" v-if="toleration.operator!='Exists'" v-model="toleration.value"  />
                        <Input type="text" v-else placeholder="operator为Exists不需要value"  disabled/>
                    </Form-Item>
                    <Form-Item label="tolerationSeconds:" prop="tolerationSeconds">
                        <Input type="number" v-if="toleration.effect=='NoExecute'" v-model="toleration.tolerationSeconds"  />
                        <Input type="number" v-else placeholder="只有effect为NoExecute才需要填写"  disabled  />
                    </Form-Item>
                </Form> 
               
            </div>
            <div slot="footer">
                 <p style="text-align: left"> 说明tolerationSeconds:配合NoExecute使用，允许pod保留多久</p>
                <Button  size="default"  type="primary"   @click="submit_add_toleration">提交</Button>
            </div>
        </Modal>
    </div>
</template>
<script>
import {k8s_namespace_post} from '@/api/http'
import {  
    add_toleration,
} from  '@/api'
export default ({
    name: 'addToletationModal',
    props: {
        isShow: {
            type: Boolean
        }, 
        deploy_name: {
            type: String
        },
        // toleration:{
        //     type: Object,
        //     default: ()=>{
        //         return {}
        //     }
        // },
    },
    data() {   
        return {
            showModal: false,
            ///////////////////
            toleration:{
                effect: 'NoSchedule',
                key: '',
                value: '',
                operator: 'Equal',
                tolerationSeconds: null,
            },
            toleration_effect_list:['NoSchedule','PreferNoSchedule','NoExecute'],
            toleration_operator_list:['Equal','Exists'],
            toleration_rules: {

            },
        }
    },
    methods: {
        rightClose(){
            this.$emit('closeModal')
        },
        // 提交添加容忍事件 
        submit_add_toleration(){
            // this.showModal = false
            if (this.toleration.tolerationSeconds != null) {
                this.toleration.tolerationSeconds = parseInt( this.toleration.tolerationSeconds)
            }
            console.log(this.deploy_name)
            let data={
                "name":this.deploy_name,
                "toleration":  this.toleration
            }
            k8s_namespace_post(add_toleration,data).then( (response) => {
                let info = JSON.stringify(response.data)
                if(info.indexOf('ok') != -1) {
                    this.$Message.success('添加容忍成功')
                    this.$emit('addSuccess')
                    this.$emit('closeModal')
                }else {
                    alert(info)
                }
            })
        },
    },
    watch: {
        isShow(newv,oldv){
            console.log(oldv)
            this.showModal = newv;
        },
    },
})
</script>